package com.jdk.learn.thread;

public class ThreadVolidateDemo {

    public static void main(String[] args) {
        TValidate t=new TValidate();
        try {
            t.start();
            Thread.sleep(2000);
            t.setFlag(false);
            Thread.sleep(2000);

            System.out.println(t.flag);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

class TValidate extends Thread {
    public volatile boolean flag=true;//这种方式会终止线程执行
//    public boolean flag=true;//这种方式不会终止线程执行
    @Override
    public void run() {
        System.out.println("begin....");
        while (flag) {
//            System.out.println("-----------------------执行中-------------");
        }
        System.out.println("end....");
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
        System.out.println("设置完毕。。");
    }
}